package views;

import bean.Cls;
import service.ClassService;
import utils.CardSecretGenerator;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddClass extends JFrame {

    private static final long serialVersionUID = 1L;
    // 输入班级名称的文本框
    private JPanel contentPane;
    // 输入班级名称的文本框
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddClass frame = new AddClass();
                    frame.setVisible(true);// 显示窗口
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AddClass() {
        setTitle("新增班级"); // 设置窗口标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作
        setBounds(100, 100, 375, 300); // 设置窗口大小
        contentPane = new JPanel(); // 创建面板
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); // 设置边框

        setContentPane(contentPane); // 设置内容面板
        contentPane.setLayout(null); // 设置布局为null

        // 标签和文本框
        JLabel lblNewLabel = new JLabel("新增班级");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        lblNewLabel.setBounds(134, 10, 174, 58);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("班级名称：");
        lblNewLabel_1.setBounds(42, 84, 73, 15);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(105, 81, 203, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        // 新增按钮
        JButton btnNewButton = new JButton("新增");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardSecretGenerator csg=new CardSecretGenerator(); // 创建随机数生成器
                if(textField.getText().length()==0) {
                    JOptionPane.showMessageDialog(null,"班级名称不能为空！！","提示",JOptionPane.PLAIN_MESSAGE);
                }else {
                    //创建一个对象封装数据
                    Cls c=new Cls();
                    c.setC_id(csg.Rnumber());// 设置班级ID
                    c.setC_name(textField.getText());// 设置班级名称
                    //打印Cls对象
                    System.out.println(c);

                    ClassService cs=new ClassService();
                    int n=cs.insertClass(c);// 插入班级
                    if(n>0) {
                        JOptionPane.showMessageDialog(null,"新增班级成功！！","提示",JOptionPane.PLAIN_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(null,"新增班级失败！！","提示",JOptionPane.PLAIN_MESSAGE);
                    }
                }
            }
        });
        btnNewButton.setBounds(80, 230, 93, 23);
        contentPane.add(btnNewButton);

        // 返回按钮
        JButton btnNewButton_1 = new JButton("返回");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main main = new Main(); // 创建主界面
                main.setVisible(true); // 显示主界面
                AddClass.this.dispose(); // 关闭当前窗口
            }
        });
        btnNewButton_1.setBounds(183, 230, 93, 23);
        contentPane.add(btnNewButton_1);
    }

}
